package com.aek.aekdroid.data;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.aek.aekdroid.Helper;

public class Match {
  public String            hometeam, awayteam, tournament, channel, stadium,
      homegoals, awaygoals, period, players;
  public long              datetime, updated;
  public int               id, edited, deleted;
  public ArrayList<Detail> details = new ArrayList<Detail>();

  public Match(Element el) {
    this.id = Helper.getIntElementValue(el, "id", -1);
    this.tournament = Helper.getStringElementValue(el, "tournament", "");
    this.channel = Helper.getStringElementValue(el, "channel", "");
    this.hometeam = Helper.getStringElementValue(el, "hometeam", "");
    this.awayteam = Helper.getStringElementValue(el, "awayteam", "");
    this.stadium = Helper.getStringElementValue(el, "stadium", "");
    this.period = Helper.getStringElementValue(el, "period", "");
    String strDatetime = Helper.getStringElementValue(el, "datetime", "");
    SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yy H:m:s");
    try {
      this.datetime = sdf.parse(strDatetime).getTime();
    } catch (ParseException e) {

    }
    this.updated = Helper.getLongElementValue(el, "updated", 0) * 1000;
    this.deleted = Helper.getIntElementValue(el, "deleted", 0);
    this.homegoals = Helper.getStringElementValue(el, "homegoals", "");
    this.awaygoals = Helper.getStringElementValue(el, "awaygoals", "");
    Element detailsEl = Helper.getElement(el, "details");
    ArrayList<Element> detailEl = Helper.getElements(detailsEl, "detail");
    for (Element element : detailEl) {
      details.add(new Detail(element));
    }
    Element playersEl = Helper.getElement(el, "players");
    if (playersEl != null) {
      ArrayList<Element> playerEl = Helper.getElements(playersEl, "player");
      String tmpPlayers = "";
      for (Element element : playerEl) {
        tmpPlayers += Helper.getStringElementValue(element, "player", "") + ",";
      }
      if (tmpPlayers.length() > 1) {
        this.players = tmpPlayers.substring(0, tmpPlayers.length() - 1);
      } else {
        this.players = "";
      }
    }
  }

  public class Detail {
    public String min, player, comment;
    public int    _id, match_id, type, side, deleted;

    public Detail(Element el) {
      this._id = Helper.getIntElementValue(el, "id", -1);
      this.match_id = Match.this.id;
      this.type = Helper.getIntElementValue(el, "type", -1);
      this.min = Helper.getStringElementValue(el, "minute", "");
      this.player = Helper.getStringElementValue(el, "player", "");
      this.comment = Helper.getStringElementValue(el, "comment", "");
      this.side = Helper.getIntElementValue(el, "side", -1);
      this.deleted = Helper.getIntElementValue(el, "deleted", 0);
    }

  }

}
